package polygon.util;

import java.util.Random;

/** @author Mike Mirzayanov */
public class SessionUtil {
    /** Random. */
    private static final Random random = new Random(getSeed());

    private static long getSeed() {
        return System.nanoTime()
                + Thread.currentThread().getId() * 103
                + Runtime.getRuntime().maxMemory() * 1009
                + Runtime.getRuntime().freeMemory() * 100009
                + Runtime.getRuntime().totalMemory() * 109;
    }

    /** @return randomToken Returns random string which used as session token. */
    public static synchronized String getRandomToken() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 16; i++) {
            int item = random.nextInt(16);
            if (item < 10) {
                sb.append((char) ('0' + item));
            } else {
                sb.append((char) ('a' + item - 10));
            }
        }
        return sb.toString();
    }

    public static synchronized long getRandomLong() {
        return random.nextLong();
    }
}
